<template>
	<view class="uploadImageBox">
		<view style="display: flex;flex-wrap: wrap;">
			<view v-for="(item,imageIndex) in imageList" :key="imageIndex" class="imageAddBox" :class='{"margin0":type}'>
				<view v-if="!item.upload" class="uploadBox">
					<u-loading-icon color='#fff'></u-loading-icon>
					<view>
						上传中
					</view>
				</view>
				<image mode="aspectFill" :src="item.coverUrl" />
				<view class="del" v-if="item.upload" @click="del(item)">
					<view>
						<u-icon name="close" color="#fff" size="20rpx"></u-icon>
					</view>
				</view>
				<view v-if="item.upload" class="success">
					<view>
						<u-icon name="checkmark" color="#fff" size="20rpx"></u-icon>
					</view>
				</view>
				<view v-if="item.upload&&!type" class="title" @click="openModal(imageIndex) ">
					<u--text v-if="item.title" lines='2' size='24rpx' color='#fff' :text='item.title'></u--text>
					<u--text v-else lines='2' size='24rpx' align='center' color='#fff' text='图片描述'></u--text>
				</view>
			</view>
			<view v-if="imageList.length&&!type" class="imageAddBox" :class='{"margin0":type}' @click="upload">
				<u-icon name="camera-fill" color="#D3D4D6" size="26"></u-icon>
			</view>
		</view> <root-portal>
			<u-modal :show="show" showCancelButton @cancel='cancel' @confirm="confirm" ref="uModal">
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							图片描述
						</view>
						<u-icon name='close' @click='cancel' />
					</view>
					<u--textarea @change='textChange' :disabled="disabled" v-if='show'
						v-model="imageList[titleIndex].title" placeholder="请输入图片描述"></u--textarea>
				</view>
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		props: ['imageUrl', 'propsIndex', 'imageShow', 'type'],
		data() {
			return {
				title: null,
				titleIndex: 0,
				imageList: [

				],
				imageUpload: {
					aliyun: null,
				},
				index: 0,
				show: false,
				disabled: false,
			}
		},
		mounted() {
			if (this.imageUrl?.length) {
				this.imageList = this.imageUrl
			}
			if (this.propsIndex != 1) {
				if (this.imageShow == 'photo') {
					this.upload()
				} else if(this.imageShow == 'chat'){
					this.chatUpload()
				}
			}
		},
		methods: {
			confirm() {
				uni.$u.toast('设置成功')
				this.$emit('changeImageList', this.imageList)
				this.show = false
				this.disabled = false
				this.titleIndex = 0
			},
			cancel() {
				this.imageList[this.titleIndex].title = this.title
				this.titleIndex = 0
				this.show = false
			},
			openModal(imageIndex) {
				this.titleIndex = imageIndex
				this.title = this.imageList[imageIndex].title
				this.show = true
			},
			chatUpload() {
				wx.chooseMessageFile({
					count: this.type ? 1 : 9,
					type: 'image',
					success: (res) => {
						console.log(res)
						res.tempFiles.forEach((item, index) => {
							if (this.type) {
								this.imageList = [{
									url: item.path,
									coverUrl: item.path,
									id: null,
									upload: false,
									title: ''
								}]
							} else {
								this.imageList.push({
									url: item.path,
									coverUrl: item.path,
									id: null,
									upload: false,
									title: ''
								})
							}

						})
						this.$emit('changeImageList', this.imageList)
						const length = this.imageList.filter(item => item.upload).length
						if (length) {
							this.index = length
						}
						this.startUpload()

					},
					fail: (err) => {
						console.log(err)
						if (!this.imageList.length) {
							this.$emit('close')
						}
					}
				})
			},
			upload() {
				uni.chooseImage({
					count: this.type ? 1 : 9,
					success: (res) => {
						console.log(res)
						res.tempFilePaths.forEach((item, index) => {
							if (this.type) {
								this.imageList = [{
									url: item,
									coverUrl: item,
									id: null,
									upload: false,
									title: ''
								}]
							} else {
								this.imageList.push({
									url: item,
									coverUrl: item,
									id: null,
									upload: false,
									title: ''
								})
							}
						})
						this.$emit('changeImageList', this.imageList)
						const length = this.imageList.filter(item => item.upload).length
						if (length) {
							this.index = length
						}
						this.startUpload()
					},
					fail: () => {
						if (!this.imageList.length) {
							this.$emit('close')
						}
					}
				})
			},
			startUpload() {
				if(this.type){
					this.index=0
				}
				if (!this.imageList[this.index].url) return
				uni.uploadFile({
					url: this.$config.url + '/api/image/upload', //仅为示例，非真实的接口地址
					filePath: this.imageList[this.index].url,
					name: 'file',
					header: {
						'Content-Type': 'application/json;charset=UTF-8',
						'Authorization': "Bearer " + uni.getStorageSync('token')
					},
					success: (res) => {
						const {
							code,
							message
						} = JSON.parse(res.data)
						console.log(JSON.parse(res.data))
						if (code == 0) {
							this.imageList[this.index].upload = true
							this.imageList[this.index].small_url = JSON.parse(res.data).data.data.urlSmall
							this.imageList[this.index].url = JSON.parse(res.data).data.data.urlMedium
							this.index++
							if (this.index >= this.imageList.length) {
								uni.$u.toast('上传完成')
								this.$emit('changeImageList', this.imageList)
								return
							}
							this.startUpload()
						} else {
							uni.$u.toast(message)
							this.imageList = this.imageList.filter(item => item.upload)
						}
					},
					fail: (err) => {
						console.log(err);
						this.$emit('close')
					}
				});

			},
			del(item) {
				this.imageList = this.imageList.filter(image => image.url != item.url)
				if (!this.imageList.length) {
					this.$emit('close')
				}
				this.$emit('changeImageList', this.imageList)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uploadImageBox {
		display: flex;
	}
	.margin0{
		margin: 0 !important;
	}
	.imageAddBox {
		width: 100rpx;
		height: 100rpx;
		background-color: #f4f5f7;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		margin-right: 20rpx;
		margin-bottom: 8px;

		.success {
			z-index: 3;
			position: absolute;
			bottom: 0;
			right: 0;
			display: flex;
			flex-direction: row;
			border-style: solid;
			border-top-color: transparent;
			border-left-color: transparent;
			border-bottom-color: #5ac725;
			border-right-color: #5ac725;
			border-width: 9px;
			align-items: center;
			justify-content: center;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				bottom: -10px;
				right: -10px;
			}
		}

		.del {
			position: absolute;
			top: 0;
			right: 0;
			background-color: #373737;
			height: 14px;
			width: 14px;
			display: flex;
			flex-direction: row;
			border-bottom-left-radius: 100px;
			align-items: center;
			justify-content: center;
			z-index: 3;

			>view {
				position: absolute;
				-webkit-transform: scale(0.7);
				transform: scale(0.7);
				top: 0px;
				right: 0px;
			}
		}

		>image {
			width: 100%;
			height: 100%;
		}

		.uploadBox {
			position: absolute;
			background-color: rgba(0, 0, 0, 0.3);
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 20rpx;
			color: #fff;
			width: 100%;
			height: 100%;


		}
	}

	.title {
		position: absolute;
		width: 100rpx;
		height: 100rpx;
		background: rgba(0, 0, 0, 0.3);
		z-index: 2;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 10rpx;
		box-sizing: border-box;
	}
</style>